package com.example.myapplication.fragment;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.databinding.FragmentLoseBinding;
import com.example.myapplication.viewmodel.MyViewModel;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.Navigation;

public class LoseFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        MyViewModel myViewModel = ViewModelProviders.of(requireActivity()
                ,new SavedStateViewModelFactory(requireActivity().getApplication(),this)).get(MyViewModel.class);
        FragmentLoseBinding binding = DataBindingUtil.inflate(inflater,R.layout.fragment_lose,container,false);
        binding.setData(myViewModel);
        binding.setLifecycleOwner(requireActivity());

        Bundle arguments = getArguments();
        String trueAnswer = arguments.getString("trueAnswer");
        String wrongAnswer = arguments.getString("wrongAnswer");

        binding.tvTure.setText(trueAnswer);
        binding.tvWrong.setText(wrongAnswer);

        binding.btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Navigation.findNavController(view).navigate(R.id.action_loseFragment_to_titleFragment2);
            }
        });

        Glide.with(getContext()).load(R.mipmap.lose_cry).into(binding.imageView3);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                MediaPlayer.create(getContext(),R.raw.fail).start();
            }
        }, 500);
        return binding.getRoot();
    }
}
